CMAKE_MINIMUM_REQUIRED(VERSION 3.16.3 FATAL_ERROR)


if (POLICY CMP0068)
	cmake_policy(SET CMP0068 NEW)
	set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif ()

if(POLICY CMP0075)
	cmake_policy(SET CMP0075 NEW)
endif()

if (POLICY CMP0077)
	cmake_policy(SET CMP0077 NEW)
endif ()

#===--------------------------------------------------------------
# project setup
#===--------------------------------------------------------------

project(plc2llvm)
set(CMAKE_CXX_STANDARD 20)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

#===--------------------------------------------------------------
# llvm configure
#===--------------------------------------------------------------
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# 引入llvm/antlr以及项目头文件
include_directories(
		${LLVM_INCLUDE_DIRS}
		${PROJECT_SOURCE_DIR}/include
)

# 引入llvm宏定义
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})

# find llvm libs
llvm_map_components_to_libnames(llvm_libs support core irreader)

# 打开 exception 和RTTI
set(LLVM_ENABLE_RTTI ON)
set(LLVM_ENABLE_EH ON)

# ========== set antlr ==========
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Antlr)
add_definitions(-DANTLR4CPP_STATIC)
include(ExternalAntlr4Cpp)
include_directories(${ANTLR4_INCLUDE_DIRS})
set(ANTLR_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/antlr/antlr-4.10.1-complete.jar)
find_package(ANTLR REQUIRED)

add_subdirectory(grammar)

include_directories(
	${ANTLR_PLCSTLexer_OUTPUT_DIR}
	${ANTLR_PLCSTParser_OUTPUT_DIR}
	)

add_subdirectory(lib)

if(TEST)
	add_subdirectory(test)
endif()